<?php
/**
 * Created by PhpStorm.
 * User: apple
 * Date: 2020/10/12
 * Time: 4:06 PM
 */
namespace app\index\logic;


use app\model\Supplies;
use app\model\SuppliesType;
use app\model\Warehouse;
use think\facade\Db;
use think\facade\Request;

class WarehouseLogic
{

    public function getList()
    {
        $orgId = config('user')['orgid'];
        $where = [
            ['w_status','=',1],
            ['w_orgid','=',$orgId]
        ];
        $param = Request::get();
        if(isset($param['name']) || !empty($param['name'])){
            $where[] = ['s_name','like','%'.trim($param['name']).'%'];
        }
        $warehouseModel = new Warehouse();
        $list = $warehouseModel->getWarehouseTypeList($where);

        if($list){
            foreach ($list as $k=>$item){
                $listWhere = [
                    's_type' => $item['type'],
                    'w_orgid' => $orgId,
                ];
                $list[$k]['list'] = $warehouseModel->getWarehouseList($listWhere) ?? [];
            }
        }
        return $list;
    }

    public function update()
    {
        $param = Request::post();

        $data = [
            'w_uid' => config('user.id'),
            'w_update_time' => time(),
        ];

        if(!isset($param['warehouseId']) || empty($param['warehouseId'])) fail('请选择商品库存信息');
        $warehouseId = intval($param['warehouseId']);
        $warehouseModel = new Warehouse();
        $warehouseInfo = $warehouseModel->getWarehouseInfoById($warehouseId);
        if(!$warehouseInfo)  fail('库存信息不存在');

        if(!isset($param['stock']) || empty($param['stock']) || !is_numeric($param['stock'])) fail('请输入正确的库存数');
        $data['w_stock'] = intval($param['stock']);

        $rt = $warehouseModel->where(['w_id'=>$warehouseId])->update($data);
        if(!$rt)    fail('商品库存修改失败');
        return true;

    }

}